package com.synnex.mobile.servier.core.notification.dao.impl;

import com.synnex.mobile.generic.dao.impl.GenericHibernateDaoImpl;
import com.synnex.mobile.servier.core.msgBoard.dto.MsgDTO;
import com.synnex.mobile.servier.core.notification.dao.DeviceTokenDao;
import com.synnex.mobile.servier.core.notification.dao.NotificationDao;
import com.synnex.mobile.servier.core.notification.dto.DeviceToken;
import com.synnex.mobile.servier.core.notification.dto.Notification;
import com.synnex.mobile.servier.web.utils.DateUtil;
import org.hibernate.Criteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Projections;
import org.hibernate.criterion.Restrictions;

import java.util.Date;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User:kerryf
 * To change this template use File | Settings | File Templates.
 */
public class NotificationDaoImpl extends GenericHibernateDaoImpl<Notification, Integer> implements NotificationDao {

    public List<Notification> getNotifications(int begin, int size,String from) {

        Criteria c = this.getSession().createCriteria(Notification.class);

        if("mobile".equals(from)){
            c.add(Restrictions.isNotNull("actualSendTime"));
        }
        c.addOrder(Order.desc("actualSendTime"));
        c.setFirstResult(begin).setMaxResults(size);
        return c.list();
    }

    public Integer countNotification(String from) {

        Criteria c = this.getSession().createCriteria(Notification.class);
        if("mobile".equals(from)){
            c.add(Restrictions.isNotNull("actualSendTime"));
        }
        Integer count = (Integer) c.setProjection(Projections.count("id")).uniqueResult();

        return count;
    }

    public void addNotification(Notification notification) {

        this.save(notification);
    }

    public void updateNotification(Notification notification) {
        super.update(notification);
    }

    public void deleteNotify(Integer id) {
        super.delete(super.get(id));
    }

    public Notification getNotificationById(Integer id) {
        return super.get(id);
    }

    public List<Notification> getNotifications(Date old, Date future) {
        System.out.println("old"+ DateUtil.dateToStr(old,"yyyy-MM-dd HH:mm:ss"));
        System.out.println("old"+ DateUtil.dateToStr(future,"yyyy-MM-dd HH:mm:ss"));
        Criteria c  = this.getSession().createCriteria(Notification.class);
        c.add(Restrictions.between("planSendTime",old,future))
//               c .add(Restrictions.gt("planSendTime", old))
//                .add(Restrictions.lt("planSendTime",future))
         .add(Restrictions.eq("sendNow", "N"))
                . add(Restrictions.isNull("actualSendTime"));
        return c.list();
    }
}
